package com.eden.door.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;

import com.eden.door.component.AddOrderButton;
import com.eden.door.component.CalculateButton;
import com.eden.door.component.MenuButton;
import com.eden.door.context.AppContext;

/**
 * 程序的主页面
 * 
 * @author Eden
 * 
 */
public class MainView {

	private JFrame frame;

	private JPanel jplMain;
	private JPanel jplContent;
	private JPanel jplDoorButton;

	private JPanel jplBottom;
	private JButton jbtPrint;
	private JButton jbtCancel;
	private JPanel jplMenu;
	private JButton jbtCalculate;
	private JButton jbtAddOrder;

	private JButton jbtMenu;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainView window = new MainView();
				
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public MainView() {
		initialize();
		AppContext.instance().setMainView(this) ;
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {

		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (Exception e) {
		}
		frame = new JFrame();
		frame.setIconImage(Toolkit.getDefaultToolkit().getImage(
				"image\\icon.png"));
		frame.setBounds(100, 100, 950, 620);
		frame.setMinimumSize(new Dimension(700, 460));
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(new GridLayout(0, 1, 0, 0));

		// 主面板,放置所有的组件
		jplMain = new JPanel();
		jplMain.setBackground(Color.GRAY);
		frame.getContentPane().add(jplMain);

		GridBagLayout gbl_jplMain = new GridBagLayout();
		gbl_jplMain.rowWeights = new double[] { 0.0, 1.0, 0.0 };
		gbl_jplMain.rowHeights = new int[] { 43, 483, 41 };
		gbl_jplMain.columnWeights = new double[] { 0, 1.0, 0.0 };
		gbl_jplMain.columnWidths = new int[] { 39, 762, 158 };
		jplMain.setLayout(gbl_jplMain);

		// 放置右上角菜单面板
		jplMenu = new JPanel();
		jplMenu.setBackground(Color.GRAY);
		GridBagConstraints gbc_jplMenu = new GridBagConstraints();
		gbc_jplMenu.insets = new Insets(0, 0, 1, 0);
		gbc_jplMenu.fill = GridBagConstraints.BOTH;
		gbc_jplMenu.gridx = 2;
		gbc_jplMenu.gridy = 0;
		jplMain.add(jplMenu, gbc_jplMenu);
		jplMenu.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));

		jbtCalculate = new CalculateButton("计算");
		jbtCalculate.setPreferredSize(new Dimension(77, 33));

		// 放置门的按钮和计算时的面板
		jplContent = new JPanel();
		GridBagConstraints gbc_jplContent = new GridBagConstraints();
		gbc_jplContent.fill = GridBagConstraints.BOTH;
		gbc_jplContent.insets = new Insets(0, 0, 1, 0);
		gbc_jplContent.gridx = 1;
		gbc_jplContent.gridy = 1;
		gbc_jplContent.gridwidth = 2;
		jplMain.add(jplContent, gbc_jplContent);

		// 最下部的面板
		jplBottom = new JPanel();
		jplBottom.setBackground(new Color(255, 228, 181));
		GridBagConstraints gbc_jplBottom = new GridBagConstraints();
		gbc_jplBottom.fill = GridBagConstraints.BOTH;
		gbc_jplBottom.gridx = 1;
		gbc_jplBottom.gridy = 2;
		gbc_jplBottom.gridwidth = 2;
		jplMain.add(jplBottom, gbc_jplBottom);
		jplBottom.setLayout(new FlowLayout(FlowLayout.RIGHT, 15, 5));

		// 打印按钮
		jbtPrint = new JButton("打印");
		jbtPrint.setPreferredSize(new Dimension(85, 33));

		// 下单
		jbtAddOrder = new AddOrderButton("下单");
		jbtAddOrder.setPreferredSize(new Dimension(85, 33));
		// 回主菜单按钮
		jbtCancel = new JButton("回主菜单");
		jbtCancel.setPreferredSize(new Dimension(85, 33));

		jbtCancel.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				jplContent.removeAll();
				jplContent.setLayout(new BorderLayout());
				jplContent.add(jplDoorButton, BorderLayout.CENTER);

				jplBottom.remove(jbtCalculate);
				jplBottom.remove(jbtAddOrder);

				jplContent.repaint();
				jplBottom.repaint();
				jplContent.validate(); 
				jplBottom.validate();
			}
		});

		jplMenu.add(jbtCancel);

		jbtMenu = new MenuButton(this , "菜单");
		jbtMenu.setPreferredSize(new Dimension(57, 33));
		jplMenu.add(jbtMenu);

		// 根据门的种类加入相应按钮
		jplDoorButton = new DoorButtonsPanel(this);
		jplContent.setLayout(new BorderLayout());
		jplContent.add(jplDoorButton, BorderLayout.CENTER);

	}

	// -------------getter setter method-----------------------
	public JFrame getFrame() {
		return frame;
	}

	public void setFrame(JFrame frame) {
		this.frame = frame;
	}

	public JPanel getJplMain() {
		return jplMain;
	}

	public void setJplMain(JPanel jplMain) {
		this.jplMain = jplMain;
	}

	public JPanel getJplContent() {
		return jplContent;
	}

	public void setJplContent(JPanel jplContent) {
		this.jplContent = jplContent;
	}

	public JPanel getJplDoorButton() {
		return jplDoorButton;
	}

	public void setJplDoorButton(JPanel jplDoorButton) {
		this.jplDoorButton = jplDoorButton;
	}

	public JPanel getJplBottom() {
		return jplBottom;
	}

	public void setJplBottom(JPanel jplBottom) {
		this.jplBottom = jplBottom;
	}

	public JButton getJbtCancel() {
		return jbtCancel;
	}

	public void setJbtCancel(JButton jbtCancel) {
		this.jbtCancel = jbtCancel;
	}

	public JButton getJbtPrint() {
		return jbtPrint;
	}

	public void setJbtPrint(JButton jbtPrint) {
		this.jbtPrint = jbtPrint;
	}

	public JButton getJbtCalculate() {
		return jbtCalculate;
	}

	public void setJbtCalculate(JButton jbtCalculate) {
		this.jbtCalculate = jbtCalculate;
	}
	
	public JButton getJbtAddOrder(){
		return jbtAddOrder ;
	}

	public JPanel getJplMenu() {
		return jplMenu;
	}

	public void setJplMenu(JPanel jplMenu) {
		this.jplMenu = jplMenu;
	}
}
